#!/bin/bash
# set -x
# This script runs in proxy server which has vpn connection to 
# the internet. It will delete default route which is added from vpn connection
# and act as a router to forward packets from lan hosts to vpn server.

# function check route table
# if /etc/iproute2/rt_tables has table $1, return 0
# else create table $1 and return 1
# $1: table id
# $2: table name
function check_rt_tables() {
	local table_id=$1
	local table_name=$2
	local table=$(cat /etc/iproute2/rt_tables | grep $table_name | awk '{print $1}')
	if [ -z $table ]; then
		echo "Create route table $table_name"
		echo "$table_id $table_name" >> /etc/iproute2/rt_tables
		return 1
	else
		echo "Route table $table_name exists"
		return 0
	fi
}

# function check rule
# if rule $1 exists, delete it
# else create rule $1 with from ip
# $1: table
# $2: from ip
function check_rule() {
	local table_name=$1
	local from_ip=$2
	local rule_id=$(ip rule | grep $table_name | awk '{print $1}')
	# if rule exists, delete it
	if [ -n "$rule_id" ]; then
		echo "Delete rule $table_name"
		ip rule del lookup $table_name
	fi
	ip rule add from $from_ip lookup $table_name
}

# function flush route table
# if route table is not empty, flush it
# $1: table name
function flush_route_table() {
	local table_name=$1
	local route=$(ip route show table $table_name)
	if [ -n "$route" ]; then
		echo "Flush route table $table_name"
		ip route flush table $table_name
	fi
}

# function find default gateway
# return default gateway
function find_default_gateway() {
	local default_gateway=$(ip route | grep default | awk '{print $3}')
	echo $default_gateway
}

# function find tun1 gateway
# return tun1 gateway
function find_tun1_gateway() {
	local tun1_line=$(ip route | grep 'tun1')
	# gateway is after 'via', extract ip address after 'via'
	local tun1_gateway=$(echo $tun1_line | awk -F 'via' '{print $2}' | awk '{print $1}')
	echo $tun1_gateway
}

# function delete route
# if route $1 dev $2 exists, delete it
# $1: destination
# $2: dev
# $3: table name default: main
function delete_route() {
	local destination=$1
	local dev=$2
	local table_name=$3
	if [ -z "$table_name" ]; then
		table_name="main"
	fi
	local route=$(ip route show table $table_name | grep $destination | grep $dev)
	if [ -n "$route" ]; then
		echo "Delete route $destination dev $dev"
		ip route del $destination dev $dev table $table_name
	fi
}

# function get last number of ipv4 address
# $1: ipv4 address
# return last number of ipv4 address
function get_last_number() {
	local ip=$1
	local last_number=$(echo $ip | awk -F '.' '{print $4}')
	echo $last_number
}

PROXY_IP="10.2.12.85"
VPN_NETWORK="172.168.0.0/16"

# function create route table from ip address
# if ip address is 10.2.12.101, table name is table_101
# then check route table existence
# then check rule existence
# if rule not exists, add rule with from ip address lookup table name
# $1: ip address
function create_route_table_from_ip() {
	local ip=$1
	local table_name="table_$(get_last_number $ip)"
	check_rt_tables $(get_last_number $ip) $table_name
	check_rule $table_name $ip
	# add default route through dev tun1 via tun1 gateway
	local tun1_gateway=$(find_tun1_gateway)
	ip route add default dev tun1 via $tun1_gateway table $table_name
}

# function check iptables rules
# if iptables nat table rules not exists, add them
# source: source ip address
function add_nat_iptables_rules() {
	local source=$1
	local iptables_nat=$(iptables -t nat -L -n | grep $source)
	if [ -z "$iptables_nat" ]; then
		echo "Add iptables rules for $source"
		iptables -t nat -A POSTROUTING -s $source -o tun1 -j MASQUERADE
	fi
}

# delete 0.0.0.0/1 route
delete_route 0.0.0.0/1 tun1
# delete 128.0.0.0/1 route
delete_route 128.0.0.0/1 tun1

servers="10.2.12.101 10.2.12.107 10.2.12.130 10.2.12.174 10.2.12.139"
for server in $servers; do
	echo "Create route table for $server"
	create_route_table_from_ip $server
	echo "Add iptables rules for $server"
	add_nat_iptables_rules $server
done

